/*     */ package com.zimbra.cs.index;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.mailbox.Flag;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import java.io.IOException;
/*     */ import java.util.Comparator;
/*     */ import java.util.List;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class FilteredQueryResults
/*     */   implements ZimbraQueryResults
/*     */ {
/*     */   private final ZimbraQueryResults results;
/*     */   private final SearchParams searchParams;
/*  41 */   private boolean filterTagDeleted = false;
/*  42 */   private boolean filterTagMuted = false;
/*  43 */   private Set<TaskHit.Status> allowedTaskStatuses = null;
/*     */   
/*     */ 
/*  46 */   private ZimbraHit firstHit = null;
/*  47 */   private ZimbraHit endHit = null;
/*  48 */   private Comparator<ZimbraHit> comp = null;
/*     */   
/*     */   FilteredQueryResults(ZimbraQueryResults other, SearchParams params)
/*     */   {
/*  52 */     this.results = other;
/*  53 */     this.searchParams = params;
/*     */     
/*  55 */     boolean isContactSearch = false;
/*  56 */     if ((this.searchParams.getTypes() != null) && (this.searchParams.getTypes().size() == 1) && (this.searchParams.getTypes().contains(MailItem.Type.CONTACT)))
/*     */     {
/*  58 */       if (this.results.getSortBy() != null) {
/*  59 */         switch (this.results.getSortBy()) {
/*     */         case NAME_ASC: 
/*     */         case NAME_DESC: 
/*  62 */           isContactSearch = true;
/*  63 */           break;
/*     */         }
/*     */         
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*  70 */     if ((isContactSearch) && (this.searchParams != null) && (this.searchParams.getCursor() != null)) {
/*  71 */       if (this.searchParams.getCursor().getSortValue() != null) {
/*  72 */         this.firstHit = new ResultsPager.CursorHit(this.results, this.searchParams.getCursor().getSortValue(), this.searchParams.getCursor().getItemId().getId());
/*     */       }
/*     */       
/*  75 */       if (this.searchParams.getCursor().getEndSortValue() != null) {
/*  76 */         this.endHit = new ResultsPager.CursorHit(this.results, this.searchParams.getCursor().getEndSortValue(), 0);
/*     */       }
/*     */       
/*  79 */       this.comp = this.searchParams.getSortBy().getHitComparator(this.searchParams.getLocale());
/*     */     }
/*     */   }
/*     */   
/*     */   void setFilterTagDeleted(boolean value)
/*     */   {
/*  85 */     this.filterTagDeleted = value;
/*     */   }
/*     */   
/*     */   void setFilterTagMuted(boolean value)
/*     */   {
/*  90 */     this.filterTagMuted = value;
/*     */   }
/*     */   
/*     */   void setAllowedTaskStatuses(Set<TaskHit.Status> allowed) {
/*  94 */     this.allowedTaskStatuses = allowed;
/*     */   }
/*     */   
/*     */   public long getCursorOffset()
/*     */   {
/*  99 */     return this.results.getCursorOffset();
/*     */   }
/*     */   
/*     */   public void close() throws IOException
/*     */   {
/* 104 */     this.results.close();
/*     */   }
/*     */   
/*     */   public List<QueryInfo> getResultInfo()
/*     */   {
/* 109 */     return this.results.getResultInfo();
/*     */   }
/*     */   
/*     */   public SortBy getSortBy()
/*     */   {
/* 114 */     return this.results.getSortBy();
/*     */   }
/*     */   
/*     */   public void resetIterator() throws ServiceException
/*     */   {
/* 119 */     this.results.resetIterator();
/*     */   }
/*     */   
/*     */   public ZimbraHit skipToHit(int hitNo) throws ServiceException
/*     */   {
/* 124 */     resetIterator();
/* 125 */     for (int i = 0; i < hitNo; i++) {
/* 126 */       if (!hasNext()) {
/* 127 */         return null;
/*     */       }
/* 129 */       getNext();
/*     */     }
/* 131 */     return getNext();
/*     */   }
/*     */   
/*     */   public ZimbraHit getNext() throws ServiceException
/*     */   {
/* 136 */     ZimbraHit toRet = peekNext();
/* 137 */     if (toRet != null) {
/* 138 */       this.results.getNext();
/*     */     }
/* 140 */     return toRet;
/*     */   }
/*     */   
/*     */   public boolean hasNext() throws ServiceException
/*     */   {
/* 145 */     return peekNext() != null;
/*     */   }
/*     */   
/*     */   public boolean isPreSorted()
/*     */   {
/* 150 */     return this.results.isPreSorted();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private boolean shouldFilter(ZimbraHit hit)
/*     */     throws ServiceException
/*     */   {
/* 158 */     if ((this.allowedTaskStatuses != null) && 
/* 159 */       ((hit instanceof TaskHit)) && 
/* 160 */       (!this.allowedTaskStatuses.contains(((TaskHit)hit).getStatus()))) {
/* 161 */       return true;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 166 */     if ((this.filterTagDeleted) && (hit.isLocal())) {
/* 167 */       MailItem item = hit.getMailItem();
/* 168 */       if ((item.getFlagBitmask() & Flag.BITMASK_DELETED) != 0) {
/* 169 */         return true;
/*     */       }
/*     */     }
/*     */     
/* 173 */     if ((this.filterTagMuted) && (hit.isLocal())) {
/* 174 */       MailItem item = hit.getMailItem();
/* 175 */       if ((item.getFlagBitmask() & Flag.BITMASK_MUTED) != 0) {
/* 176 */         return true;
/*     */       }
/*     */     }
/*     */     
/* 180 */     if ((hit instanceof ProxiedHit)) {
/* 181 */       if ((this.firstHit != null) && (this.comp != null) && (this.comp.compare(hit, this.firstHit) < 0)) {
/* 182 */         return true;
/*     */       }
/* 184 */       if ((this.endHit != null) && (this.comp != null) && (this.comp.compare(hit, this.endHit) >= 0)) {
/* 185 */         return true;
/*     */       }
/*     */     }
/*     */     
/* 189 */     return false;
/*     */   }
/*     */   
/*     */   public ZimbraHit peekNext()
/*     */     throws ServiceException
/*     */   {
/* 195 */     ZimbraHit cur = this.results.peekNext();
/* 196 */     while (cur != null) {
/* 197 */       boolean filterThisHit = false;
/* 198 */       if ((cur instanceof ConversationHit)) {
/* 199 */         ConversationHit ch = (ConversationHit)cur;
/* 200 */         for (MessageHit mh : ch.getMessageHits()) {
/* 201 */           filterThisHit = shouldFilter(mh);
/* 202 */           if (!filterThisHit)
/*     */             break;
/*     */         }
/*     */       } else {
/* 206 */         filterThisHit = shouldFilter(cur);
/*     */       }
/*     */       
/* 209 */       if (!filterThisHit) {
/* 210 */         return cur;
/*     */       }
/* 212 */       this.results.getNext();
/* 213 */       cur = this.results.peekNext();
/*     */     }
/* 215 */     return null;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/index/FilteredQueryResults.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */